GDAL Python 学习笔记 - 0 - 安装

GDAL Python 学习笔记 - 0 - 安装

安装(windows)

不建议自己编译安装,因为编译环境、版本等问题,会出现各种错误

使用Anaconda

conda install -c conda-forge gdal

不仅会安装GDAL的Python绑定,也会安装相应的gdal核心文件

也可以前往 lfd.uci.edu gdal 下载相应的 whl 包安装

>cd D:\ProgramData\Anaconda3\Scripts
>pip install GDAL-2.1.2-cp36-cp36m-win32.whl # 注意 Anaconda 的 Python版本与 whl 文件对应版本

可能需要 VC++ 14 / Microsoft Build Tools 2015

https://blog.csdn.net/ivan_ljf/article/details/77005083

其他方式

下载与所使用的系统相匹配的 GDAL的windows安装包。主要有三种包含gdal二进制包的下载源:GISInternalsMS4W (MapServer for Windows)OSGeo4W

GISInternals

由Tamas Szekeres 根据 MapServer 和 GDAL 最新代码构建并维护的网站,包括 Stable Releases - Stable Branches - Development Versions - Development Kits - Archive Versions

  • 选择下载安装包
    一般是以稳定版(Stable Releases)就ok
    GDAL 2.1.0 and MapServer 7.0.1
    选择最后一项:MSVC 2013x64 release-1800-x64-gdal-2-1-0-mapserver-7-0-1,然后选择gdal核心包:gdal-201-1800-x64-core.msi及相应python版本的python扩展,如GDAL-2.1.0.win-amd64-py2.7.msi
    gdal安装包选择

  • 安装
    首先安装核心包(core)。gdal默认安装到C:\Program Files\GDAL,安装时可以选择安装C#、java等语言绑定及HDF等相关组件。
    gdal安装

    gdal安装组件选择

    然后安装python组件(与环境变量中设置的python绑定)。
    安装python组件

  • 添加系统环境变量

    1. 将安装目录下的 bin文件夹 (或C:\Program Files\GDAL)加入到系统的PATH环境变量之后, 注意,path环境变量的各部分的值是以半角状态下的冒号;分割的。需添加的环境变量如:C:\gdalwin32-1.7\bin

    2. 添加变量名为GDAL_DATA的环境变量,值为C:\gdalwin32-1.7\data(或C:\Program Files\GDAL\gdal-data

    3. 测试

      >>> from osgeo import gdal
      >>> from osgeo import ogr
      >>> from osgeo import osr
      >>> from osgeo import gdal_array
      >>> from osgeo import gdalconst

    其中,from osgeo import gdal_array可能会报错(import gdalnumeric同样):

anaconda下安装的gdal没有报错

原因可能是SWIG版本问题。

gdal_array报错
低版本gdal可能语句不一样:
If you are using GDAL 1.7 bindings, you should update your imports to utilize the usage above, but the following will work until at least GDAL 2.1.

>>> import gdal
>>> import ogr
>>> import osr
>>> import gdalnumeric
>>> import gdalconst

MS4W

前往官网下载安装文件:http://www.ms4w.com/
安装之后访问 Using the Python GDAL Module 使用python模块。MS4W的python模块使用需要2.6以上版本的python,且添加了python的环境变量。
python gdal 模块安装目录为 /ms4w/python/gdal/。使用:

  • 打开命令行窗口
  • cd 到 ms4w目录
  • 运行 setenv.bat
  • 执行python根据方法,例如
D:\Program Files\ms4w>gdal_merge.py
No input files selected.
Usage: gdal_merge.py [-o out_filename] [-of out_format] [-co NAME=VALUE]*
[-ps pixelsize_x pixelsize_y] [-tap] [-separate] [-q] [-v] [-pct]
[-ul_lr ulx uly lrx lry] [-init "value [value...]"]
[-n nodata_value] [-a_nodata output_nodata_value]
[-ot datatype] [-createonly] input_files
[--help-general]

其他

参考: https://pypi.python.org/pypi/GDAL/

If you have previous code that imported the global module and still need to support the old import, a simple try…except import can silence the deprecation warning and keep things named essentially the same as before::

>>> try:
... from osgeo import gdal
... except ImportError:
... import gdal

  • Docstrings

    Currently, only the OGR module has docstrings which are generated from the C/C++ API doxygen materials. Some of the arguments and types might not match up exactly with what you are seeing from Python, but they should be enough to get you going. Docstrings for GDAL and OSR are planned for a future
    release.

  • Numpy/Numeric


One advanced feature of the GDAL Python bindings not found in the other language bindings (C#, Perl) is integration with the Python numerical array facilities. The gdal.Dataset.ReadAsArray() method can be used to read raster data as numerical arrays, ready to use with the Python numerical array capabilities.

These facilities have evolved somewhat over time. In the past the package was known as “Numeric” and imported using “import Numeric”. A new generation is imported using “import numpy”. Currently the old generation bindings only support the older Numeric package, and the new generation bindings only support the new generation numpy package. They are mostly compatible, and by importing gdalnumeric (or osgeo.gdal_array) you will get whichever isappropriate to the current bindings type.

  • Examples

One example of GDAL/numpy integration is found in the val_repl.py_ script.

  • Performance Notes

ReadAsArray expects to make an entire copy of a raster band or dataset unless the data are explicitly subsetted as part of the function call. For large data, this approach is expected to be prohibitively memory intensive.


.. _GDAL API Tutorial: http://www.gdal.org/gdal_tutorial.html
.. _GDAL Windows Binaries: http://gisinternals.com/sdk/
.. _Microsoft Knowledge Base doc: http://support.microsoft.com/kb/310519
.. _Python Cheeseshop: http://pypi.python.org/pypi/GDAL/
.. _val_repl.py: http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/samples/val_repl.py
.. _GDAL: http://www.gdal.org
.. _SWIG: http://www.swig.org

文章目录
  1. 1. GDAL Python 学习笔记 - 0 - 安装
    1. 1.1. 安装(windows)
      1. 1.1.1. 使用Anaconda
      2. 1.1.2. 其他方式
        1. 1.1.2.1. GISInternals
        2. 1.1.2.2. MS4W
        3. 1.1.2.3. 其他
|